package com.ufrn_access;

import com.service.SigaaCalls;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
import android.text.Editable;
import android.text.TextWatcher;

public class DetalhesDisciplinaActivity extends Activity {
	
	public static final String PREFS_NAME = "MyPrefsFile";
	static SharedPreferences settings;
	SharedPreferences.Editor editor;
	
	
	EditText editTextNota1;
	EditText editTextNota2;
	EditText editTextNota3;
	EditText editTextNota4;
	EditText editTextResultado;
	
	SigaaCalls sig;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detalhes_disciplina);
		// -------------------------------------------------------------

		Intent intent = getIntent();
		String nomedisciplina = intent.getStringExtra("nome_disciplina");

		TextView v = (TextView) findViewById(R.id.detalhes_textView_nome_disciplina);
		v.setText(nomedisciplina);
		// --------------------------------------------------------------
		TextWatcher internalTextWatcher = new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
				atualizarNotas();
			}
		};

		editTextNota1 = (EditText) findViewById(R.id.detalhes_EditTextNota1);
		editTextNota1.setText("0");
		editTextNota1.addTextChangedListener(internalTextWatcher);

		editTextNota2 = (EditText) findViewById(R.id.detalhes_EditTextNota2);
		editTextNota2.setText("0");
		editTextNota2.addTextChangedListener(internalTextWatcher);

		editTextNota3 = (EditText) findViewById(R.id.detalhes_EditTextNota3);
		editTextNota3.setText("0");
		editTextNota3.addTextChangedListener(internalTextWatcher);

		editTextNota4 = (EditText) findViewById(R.id.detalhes_EditTextNota4);
		editTextNota4.setText("0");
		editTextNota4.addTextChangedListener(internalTextWatcher);
		editTextNota4.setEnabled(false);
		
		editTextResultado = (EditText) findViewById(R.id.detalhes_EditTextResultado);
		//------------------------------------------------------------
		
		settings = getSharedPreferences(PREFS_NAME,0);
		editor = settings.edit();

		String username = settings.getString("username", "-");
		String password = settings.getString("password", "-");
		
		sig = SigaaCalls.getInstance();
		
		double[] res = sig.getDisciplinaNotas(username, password, nomedisciplina);
		if (res != null){
			editTextNota1.setText(Double.toString(res[0]));
			editTextNota2.setText(Double.toString(res[1]));
			editTextNota3.setText(Double.toString(res[2]));
			editTextNota4.setText(Double.toString(res[3]));
		}

		// ------------------------------------------------------------

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private void atualizarNotas() {

		String stringnota1 = editTextNota1.getText().toString();
		String stringnota2 = editTextNota2.getText().toString();
		String stringnota3 = editTextNota3.getText().toString();
		String stringnota4 = editTextNota4.getText().toString();
		

		if (!stringnota1.equals("") && !stringnota2.equals("") && !stringnota3.equals("") && !stringnota4.equals("")) {

			double nota1 = Double.parseDouble(stringnota1.toString());
			double nota2 = Double.parseDouble(stringnota2.toString());
			double nota3 = Double.parseDouble(stringnota3.toString());
			double nota4 = Double.parseDouble(stringnota4.toString());
			
			System.out.println("notas: " + nota1+ "--" + nota2+"--" + nota3);

			double resultado = (nota1 * 4 + nota2 * 5 + nota3 * 6) / 15;
			
			if (resultado >= 3 && resultado < 7){
				editTextNota4.setEnabled(true);
				resultado = resultado + nota4 / 2;
			}
			else{
				editTextNota4.setEnabled(false);
			}
			
			

			editTextResultado.setText(Double.toString(resultado));
		}

	}

}
